﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;
using aaaSoft.SkinEngine.SkinHelpers;
using System.Diagnostics;

namespace aaaSoft.SkinEngine.MyControls
{
    class MyForm : NativeWindow, IMyControl
    {
        Form frmBase;
        SkinEngine skinEng;

        List<String> typeNames = new List<String>();
        public MyForm(Form frm, SkinEngine skinEng)
        {
            frmBase = frm;
            this.skinEng = skinEng;
            LoadDefaultSkin();

            typeNames.Add(typeof(Label).ToString());
            typeNames.Add(typeof(Panel).ToString());
            typeNames.Add(typeof(GroupBox).ToString());
            typeNames.Add(typeof(TableLayoutPanel).ToString());
            typeNames.Add(typeof(PictureBox).ToString());
        }

        public void StopControlSkin()
        {
            frmBase.Paint -= new PaintEventHandler(frmBase_Paint);
            frmBase.SizeChanged -= new EventHandler(frmBase_SizeChanged);
            frmBase.MouseMove -= new MouseEventHandler(frmBase_MouseMove);
            frmBase.MouseLeave -= new EventHandler(frmBase_MouseLeave);
            frmBase.MouseDown -= new MouseEventHandler(frmBase_MouseDown);
            frmBase.MouseUp -= new MouseEventHandler(frmBase_MouseUp);
            frmBase.ControlAdded -= new ControlEventHandler(frmBase_ControlAdded);
            frmBase.ControlRemoved -= new ControlEventHandler(frmBase_ControlRemoved);

            frmBase.Region = null;

            RemoveControlsMoveEvent(frmBase);
            ReleaseHandle();
        }


        public void StartControlSkin()
        {
            frmBase.Paint += new PaintEventHandler(frmBase_Paint);
            frmBase.SizeChanged += new EventHandler(frmBase_SizeChanged);
            frmBase.MouseMove += new MouseEventHandler(frmBase_MouseMove);
            frmBase.MouseLeave += new EventHandler(frmBase_MouseLeave);
            frmBase.MouseDown += new MouseEventHandler(frmBase_MouseDown);
            frmBase.MouseUp += new MouseEventHandler(frmBase_MouseUp);
            frmBase.ControlAdded += new ControlEventHandler(frmBase_ControlAdded);
            frmBase.ControlRemoved += new ControlEventHandler(frmBase_ControlRemoved);

            skinEng.BackColorChanged += new EventHandler(skinEng_BackColorChanged);

            if (!skinEng.IsDesignMode)
            {
                SetRoundRectWnd();
            }
            AddControlsMoveEvent(frmBase);
            AssignHandle(frmBase.Handle);
        }

        void frmBase_ControlRemoved(object sender, ControlEventArgs e)
        {
            RemoveControlsMoveEvent(e.Control);
        }

        void frmBase_ControlAdded(object sender, ControlEventArgs e)
        {
            AddControlsMoveEvent(e.Control);
        }

        protected override void WndProc(ref Message m)
        {
            base.WndProc(ref m);
        }

        //所有指定类型控件关联上窗体的移动
        private void AddControlsMoveEvent(Control crl)
        {
            string ctlTypeName = crl.GetType().ToString();
            if (typeNames.Contains(ctlTypeName))
            {
                crl.MouseDown += new MouseEventHandler(frmBase_MouseDown);
                crl.MouseMove += new MouseEventHandler(frmBase_MouseMove);
                crl.MouseUp += new MouseEventHandler(frmBase_MouseUp);
            }

            foreach (Control subCrl in crl.Controls)
            {

                AddControlsMoveEvent(subCrl);
            }
        }

        //取消所有指定类型控件关联上窗体的移动
        private void RemoveControlsMoveEvent(Control crl)
        {
            string ctlTypeName = crl.GetType().ToString();
            if (typeNames.Contains(ctlTypeName))
            {
                crl.MouseDown -= new MouseEventHandler(frmBase_MouseDown);
                crl.MouseMove -= new MouseEventHandler(frmBase_MouseMove);
                crl.MouseUp -= new MouseEventHandler(frmBase_MouseUp);
            }
            foreach (Control subCrl in crl.Controls)
            {

                RemoveControlsMoveEvent(subCrl);
            }
        }

        void frmBase_MouseUp(object sender, MouseEventArgs e)
        {
            IsMouseDown = false;
            if ((frmBase.Region != null) & (e.Button == MouseButtons.Left))
            {
                Point pt = frmBase.PointToClient(Control.MousePosition);
                if (frmBase.MinimizeBox && ShapeHelper.PointInRect(pt, this.m_rtBtnMin))
                {
                    frmBase.WindowState = FormWindowState.Minimized;
                }
                else if (frmBase.MaximizeBox && ShapeHelper.PointInRect(pt, this.m_rtBtnMax))
                {
                    if (frmBase.WindowState == FormWindowState.Normal)
                    {
                        frmBase.WindowState = FormWindowState.Maximized;
                    }
                    else
                    {
                        frmBase.WindowState = FormWindowState.Normal;
                    }
                }
                else if (ShapeHelper.PointInRect(pt, this.m_rtBtnExit))
                {
                    frmBase.Close();
                }
            }
        }

        void frmBase_MouseDown(object sender, MouseEventArgs e)
        {
            try
            {
                IsMouseDown = true;
                Point location = frmBase.PointToClient(Form.MousePosition); //e.Location e.Location不一定自窗体，可能来自窗体的子控件;
                if (((frmBase.Region != null) & (frmBase.WindowState == FormWindowState.Normal)) & ((frmBase.FormBorderStyle == FormBorderStyle.Sizable) | (frmBase.FormBorderStyle == FormBorderStyle.SizableToolWindow)))
                {
                    bool flag = true;
                    IsMouseDown = false;
                    Rectangle rect = new Rectangle(3, 0, ((frmBase.Width - (2 * (this.sideWidth + 1))) - 3) - 1, 1);
                    if (ShapeHelper.PointInRect(location, rect))
                    {
                        UnsafeNativeMethods.ReleaseCapture();
                        UnsafeNativeMethods.SendMessage(frmBase.Handle, 0x112, 0xf003L, 0L);
                    }
                    else
                    {
                        rect = new Rectangle(0, 1, 3, (((frmBase.Height - this.formTopOffset) - this.sideHeight) - 3) - 1);
                        if (ShapeHelper.PointInRect(location, rect))
                        {
                            UnsafeNativeMethods.ReleaseCapture();
                            UnsafeNativeMethods.SendMessage(frmBase.Handle, 0x112, 0xf001L, 0L);
                        }
                        else
                        {
                            rect = new Rectangle(frmBase.ClientSize.Width - 3, 1, 3, (((frmBase.Height - this.formTopOffset) - this.sideHeight) - 3) - 1);
                            if (ShapeHelper.PointInRect(location, rect))
                            {
                                UnsafeNativeMethods.ReleaseCapture();
                                UnsafeNativeMethods.SendMessage(frmBase.Handle, 0x112, 0xf002L, 0L);
                            }
                            else
                            {
                                rect = new Rectangle(3, frmBase.ClientSize.Height - 3, ((frmBase.Width - (2 * (this.sideWidth + 1))) - 3) - 1, 3);
                                if (ShapeHelper.PointInRect(location, rect))
                                {
                                    UnsafeNativeMethods.ReleaseCapture();
                                    UnsafeNativeMethods.SendMessage(frmBase.Handle, 0x112, 0xf006L, 0L);
                                }
                                else
                                {
                                    rect = new Rectangle(0, 0, 3, 1);
                                    if (ShapeHelper.PointInRect(location, rect))
                                    {
                                        UnsafeNativeMethods.ReleaseCapture();
                                        UnsafeNativeMethods.SendMessage(frmBase.Handle, 0x112, 0xf004L, 0L);
                                    }
                                    else
                                    {
                                        rect = new Rectangle(frmBase.ClientSize.Width - 3, 0, 3, 1);
                                        if (ShapeHelper.PointInRect(location, rect))
                                        {
                                            UnsafeNativeMethods.ReleaseCapture();
                                            UnsafeNativeMethods.SendMessage(frmBase.Handle, 0x112, 0xf005L, 0L);
                                        }
                                        else
                                        {
                                            rect = new Rectangle(0, frmBase.ClientSize.Height - 3, 3, 3);
                                            if (ShapeHelper.PointInRect(location, rect))
                                            {
                                                UnsafeNativeMethods.ReleaseCapture();
                                                UnsafeNativeMethods.SendMessage(frmBase.Handle, 0x112, 0xf007L, 0L);
                                            }
                                            else
                                            {
                                                rect = new Rectangle(frmBase.ClientSize.Width - 3, frmBase.ClientSize.Height - 3, 3, 3);
                                                if (ShapeHelper.PointInRect(location, rect))
                                                {
                                                    UnsafeNativeMethods.ReleaseCapture();
                                                    UnsafeNativeMethods.SendMessage(frmBase.Handle, 0x112, 0xf008L, 0L);
                                                }
                                                else
                                                {
                                                    rect = new Rectangle((frmBase.ClientSize.Width - 3) - 12, (frmBase.ClientSize.Height - 3) - 12, 12, 12);
                                                    if (((frmBase.FormBorderStyle == FormBorderStyle.Sizable) | (frmBase.FormBorderStyle == FormBorderStyle.SizableToolWindow)) & ShapeHelper.PointInRect(location, rect))
                                                    {
                                                        UnsafeNativeMethods.ReleaseCapture();
                                                        UnsafeNativeMethods.SendMessage(frmBase.Handle, 0x112, 0xf008L, 0L);
                                                    }
                                                    else
                                                    {
                                                        frmBase.Cursor = Cursors.Default;
                                                        flag = false;
                                                        IsMouseDown = true;
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                    if (flag)
                    {
                        return;
                    }
                }
                else
                {
                    frmBase.Cursor = Cursors.Default;
                }
                MouseButtons button = e.Button;
                if (button == MouseButtons.Left)
                {
                    TimeSpan span = new TimeSpan(0, 0, 0, 0, 500);
                    if ((((DateTime.Now - this.LastMouseDownTime) <= span) & (e.Location == this.LastMouesDownPostion)) & (frmBase.FormBorderStyle == FormBorderStyle.Sizable))
                    {
                        if (frmBase.MaximizeBox)
                        {
                            if (frmBase.WindowState == FormWindowState.Normal)
                            {
                                frmBase.WindowState = FormWindowState.Maximized;
                            }
                            else
                            {
                                frmBase.WindowState = FormWindowState.Normal;
                            }
                        }
                    }
                    else
                    {
                        if ((((frmBase.WindowState == FormWindowState.Normal) & !ShapeHelper.PointInRect(location, this.m_rtBtnMin)) & !ShapeHelper.PointInRect(location, this.m_rtBtnMax)) & !ShapeHelper.PointInRect(location, this.m_rtBtnExit))
                        {
                            //UnsafeNativeMethods.ReleaseCapture();
                            //UnsafeNativeMethods.SendMessage(this.Handle, 0xa1, 2L, 0L);
                        }
                        this.LastMouseDownTime = DateTime.Now;
                        this.LastMouesDownPostion = e.Location;
                    }
                }
                else if ((button == MouseButtons.Right) && (((ShapeHelper.PointInRect(location, this.TopicRectangle) & !ShapeHelper.PointInRect(location, this.m_rtBtnMin)) & !ShapeHelper.PointInRect(location, this.m_rtBtnMax)) & !ShapeHelper.PointInRect(location, this.m_rtBtnExit)))
                {
                    FormHelper.PopupSysMenu(frmBase.Handle, frmBase.PointToScreen(e.Location));
                }
                if ((frmBase.Region != null) & (e.Button == MouseButtons.Left))
                {
                    location = frmBase.PointToClient(Control.MousePosition);
                    Graphics graphics = frmBase.CreateGraphics();
                    if (frmBase.MinimizeBox)
                    {
                        if (ShapeHelper.PointInRect(location, this.m_rtBtnMin))
                        {
                            graphics.DrawImage(btnMinClickImage, this.m_rtBtnMin);
                        }
                        else
                        {
                            graphics.DrawImage(btnMinImage, this.m_rtBtnMin);
                        }
                    }
                    if (frmBase.MaximizeBox)
                    {
                        if (ShapeHelper.PointInRect(location, this.m_rtBtnMax))
                        {
                            if (frmBase.WindowState == FormWindowState.Normal)
                            {
                                graphics.DrawImage(btnMaxClickImage, this.m_rtBtnMax);
                            }
                            else
                            {
                                graphics.DrawImage(btnRestoreClickImage, this.m_rtBtnMax);
                            }
                        }
                        else if (frmBase.WindowState == FormWindowState.Normal)
                        {
                            graphics.DrawImage(btnMaxImage, this.m_rtBtnMax);
                        }
                        else
                        {
                            graphics.DrawImage(btnRestoreImage, this.m_rtBtnMax);
                        }
                    }
                    if (ShapeHelper.PointInRect(location, this.m_rtBtnExit))
                    {
                        graphics.DrawImage(btnCloseClickImage, this.m_rtBtnExit);
                    }
                    else
                    {
                        graphics.DrawImage(btnCloseImage, this.m_rtBtnExit);
                    }
                    if (ShapeHelper.PointInRect(location, m_rtIcon))
                    {
                        Point p = new Point(this.TopicRectangle.Left, topicHeight);
                        FormHelper.PopupSysMenu(frmBase.Handle, frmBase.PointToScreen(p));
                    }
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.Print("Error From MyFrom.OnMouseDown.原因:" + ex.Message);
            }
            lastMousePosition = e.Location;
        }
        void frmBase_MouseLeave(object sender, EventArgs e)
        {
            frmBase.Cursor = Cursors.Default;
        }
        void frmBase_MouseMove(object sender, MouseEventArgs e)
        {
            Point pt = e.Location;
            if (!IsShowTopic)
            {
                return;
            }
            if (IsMouseDown)
            {
                Point lastPt = lastMousePosition;
                if (ShapeHelper.PointInRect(pt, this.m_rtBtnMin) || ShapeHelper.PointInRect(pt, this.m_rtBtnMax) || ShapeHelper.PointInRect(pt, this.m_rtBtnExit))
                {
                }
                else
                {
                    if (ShapeHelper.PointInRect(lastPt, this.m_rtBtnMin) || ShapeHelper.PointInRect(lastPt, this.m_rtBtnMax) || ShapeHelper.PointInRect(lastPt, this.m_rtBtnExit))
                    {
                    }
                    else
                    {
                        Point newPos = new Point();
                        newPos.X = frmBase.Left + e.X - lastMousePosition.X;
                        newPos.Y = frmBase.Top + e.Y - lastMousePosition.Y;
                        frmBase.Location = newPos;
                    }
                }
            }
            if (frmBase.Region != null)
            {
                Graphics graphics = frmBase.CreateGraphics();
                if (frmBase.MinimizeBox)
                {
                    if (ShapeHelper.PointInRect(pt, this.m_rtBtnMin))
                    {
                        graphics.DrawImage(btnMinHoverImage, this.m_rtBtnMin);
                    }
                    else
                    {
                        graphics.DrawImage(btnMinImage, this.m_rtBtnMin);
                    }
                }
                if (frmBase.MaximizeBox)
                {
                    if (ShapeHelper.PointInRect(pt, this.m_rtBtnMax))
                    {
                        if (frmBase.WindowState == FormWindowState.Normal)
                        {
                            graphics.DrawImage(btnMaxHoverImage, this.m_rtBtnMax);
                        }
                        else
                        {
                            graphics.DrawImage(btnRestoreHoverImage, this.m_rtBtnMax);
                        }
                    }
                    else if (frmBase.WindowState == FormWindowState.Normal)
                    {
                        graphics.DrawImage(btnMaxImage, this.m_rtBtnMax);
                    }
                    else
                    {
                        graphics.DrawImage(btnRestoreImage, this.m_rtBtnMax);
                    }
                }
                if (ShapeHelper.PointInRect(pt, this.m_rtBtnExit))
                {
                    graphics.DrawImage(btnCloseHoverImage, this.m_rtBtnExit);
                }
                else
                {
                    graphics.DrawImage(btnCloseImage, this.m_rtBtnExit);
                }
                if ((sender.Equals(frmBase)) && (frmBase.WindowState == FormWindowState.Normal) & ((frmBase.FormBorderStyle == FormBorderStyle.Sizable) | (frmBase.FormBorderStyle == FormBorderStyle.SizableToolWindow)))
                {
                    Rectangle rect = new Rectangle(3, 0, ((frmBase.Width - (2 * (this.sideWidth + 1))) - 3) - 1, 1);
                    if (ShapeHelper.PointInRect(pt, rect))
                    {
                        frmBase.Cursor = Cursors.SizeNS;
                    }
                    else
                    {
                        rect = new Rectangle(0, 1, 3, (((frmBase.Height - this.formTopOffset) - this.sideHeight) - 3) - 1);
                        if (ShapeHelper.PointInRect(pt, rect))
                        {
                            frmBase.Cursor = Cursors.SizeWE;
                        }
                        else
                        {
                            rect = new Rectangle(frmBase.ClientSize.Width - 3, 1, 3, (((frmBase.Height - this.formTopOffset) - this.sideHeight) - 3) - 1);
                            if (ShapeHelper.PointInRect(pt, rect))
                            {
                                frmBase.Cursor = Cursors.SizeWE;
                            }
                            else
                            {
                                rect = new Rectangle(3, frmBase.ClientSize.Height - 3, ((frmBase.Width - (2 * (this.sideWidth + 1))) - 3) - 1, 3);
                                if (ShapeHelper.PointInRect(pt, rect))
                                {
                                    frmBase.Cursor = Cursors.SizeNS;
                                }
                                else
                                {
                                    rect = new Rectangle(0, 0, 3, 1);
                                    if (ShapeHelper.PointInRect(pt, rect))
                                    {
                                        frmBase.Cursor = Cursors.SizeNWSE;
                                    }
                                    else
                                    {
                                        rect = new Rectangle(frmBase.ClientSize.Width - 3, 0, 3, 1);
                                        if (ShapeHelper.PointInRect(pt, rect))
                                        {
                                            frmBase.Cursor = Cursors.SizeNESW;
                                        }
                                        else
                                        {
                                            rect = new Rectangle(0, frmBase.ClientSize.Height - 3, 3, 3);
                                            if (ShapeHelper.PointInRect(pt, rect))
                                            {
                                                frmBase.Cursor = Cursors.SizeNESW;
                                            }
                                            else
                                            {
                                                rect = new Rectangle(frmBase.ClientSize.Width - 3, frmBase.ClientSize.Height - 3, 3, 3);
                                                if (ShapeHelper.PointInRect(pt, rect))
                                                {
                                                    frmBase.Cursor = Cursors.SizeNWSE;
                                                }
                                                else
                                                {
                                                    rect = new Rectangle((frmBase.ClientSize.Width - 3) - 12, (frmBase.ClientSize.Height - 3) - 12, 12, 12);
                                                    if (((frmBase.FormBorderStyle == FormBorderStyle.Sizable) | (frmBase.FormBorderStyle == FormBorderStyle.SizableToolWindow)) & ShapeHelper.PointInRect(pt, rect))
                                                    {
                                                        frmBase.Cursor = Cursors.SizeNWSE;
                                                    }
                                                    else
                                                    {
                                                        frmBase.Cursor = Cursors.Default;
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            else
            {
                frmBase.Cursor = Cursors.Default;
            }
        }


        void skinEng_BackColorChanged(object sender, EventArgs e)
        {
            ReBuildSystemButton(skinEng.BackColor);
            frmBase.Invalidate();
        }

        void frmBase_SizeChanged(object sender, EventArgs e)
        {
            if (frmBase.WindowState == FormWindowState.Normal && frmBase.ClientSize.Height < topicHeight)
            {
                frmBase.ClientSize = new Size(frmBase.ClientSize.Width, topicHeight);
                return;
            }
            if (frmBase.WindowState == FormWindowState.Maximized)
            {
                this.IsMaxBefore = true;
                int num = UnsafeNativeMethods.GetWindowLong(frmBase.Handle, -16) & -12582913;
                IntPtr nNewLong = new IntPtr(num);
                UnsafeNativeMethods.SetWindowLong(frmBase.Handle, -16, nNewLong);
            }
            else if (this.IsMaxBefore)
            {
                this.IsMaxBefore = false;
                frmBase.ControlBox = true;
                frmBase.Width -= 2;
                frmBase.Height -= (this.formTopOffset - this.sideHeight) + 2;
            }
            /*
            if (this.MaximizedBounds != Screen.PrimaryScreen.WorkingArea)
            {
                this.MaximizedBounds = Screen.PrimaryScreen.WorkingArea;
            }
             */

            if ((frmBase.WindowState == FormWindowState.Maximized) | (frmBase.WindowState == FormWindowState.Minimized))
            {
                Rectangle rect = new Rectangle(0, 0, frmBase.Width, frmBase.Height);
                frmBase.Region = new Region(rect);
                this.MaxmizeLeftArg = 1;
            }
            else
            {
                if (this.MaxmizeLeftArg == 0)
                {
                    this.LastSize = frmBase.Size;
                }
                if (!skinEng.IsDesignMode)
                {
                    this.SetRoundRectWnd();
                }
            }
            frmBase.Invalidate();
        }

        void frmBase_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            g.FillRectangle(new SolidBrush(skinEng.BackColor), frmBase.ClientRectangle);
            try
            {
                DrawSkin(g);
            }
            catch { }
        }


        public void LoadDefaultSkin()
        {
            TopicTextFormat.LineAlignment = StringAlignment.Center;
            TopicTextFormat.Alignment = StringAlignment.Near;
            TopicTextFormat.FormatFlags = StringFormatFlags.NoWrap;
            topicImage = null;
            topicHeight = 30;
            TopicTextFont = new Font("Arial", 9f, FontStyle.Bold);
            TopicTextColor = Color.White;
            m_rtIcon = new Rectangle(5, 5, 20, 20);
            sysBtnWidth = 0x1b;
            sysBtnHeight = 0x11;
            btnSpeatorWidth = 0;
            rightOffset = 7;
            topOffset = 0;
            RoundWidth = 0x10;
            RoundHeight = 0x10;


            btnMinImage = ResourceHelper.GetBitmapResource("btnMin");
            btnMinHoverImage = ResourceHelper.GetBitmapResource("btnMinHover");
            btnMinClickImage = ResourceHelper.GetBitmapResource("btnMinClick");
            btnMaxImage = ResourceHelper.GetBitmapResource("btnMax");
            btnMaxHoverImage = ResourceHelper.GetBitmapResource("btnMaxHover");
            btnMaxClickImage = ResourceHelper.GetBitmapResource("btnMaxClick");
            btnRestoreImage = ResourceHelper.GetBitmapResource("btnRestore");
            btnRestoreHoverImage = ResourceHelper.GetBitmapResource("btnRestoreHover");
            btnRestoreClickImage = ResourceHelper.GetBitmapResource("btnRestoreClick");
            btnCloseImage = ResourceHelper.GetBitmapResource("btnClose");
            btnCloseHoverImage = ResourceHelper.GetBitmapResource("btnCloseHover");
            btnCloseClickImage = ResourceHelper.GetBitmapResource("btnCloseClick");

            leftSideColor = Color.FromArgb(0x3e, 0x8e, 0x99);
            rightSideColor = Color.FromArgb(0x3e, 0x8e, 0x99);
            SideLineTimes = 1.3f;
            SideBorderTimes = 0.9f;
            ReBuildSystemButton(skinEng.BackColor);
        }

        public static void ReBuildSystemButton(Color backColor)
        {
            //return;
            System.Diagnostics.Debug.Print("RebuildSystemButton()");
            if (topicImage == null)
            {
                btnMinImage = ColorHelper.ChangeImageToColorImage(BlackbtnMinImage, backColor);
                btnMinHoverImage = ColorHelper.ChangeImageToColorImage(BlackbtnMinHoverImage, backColor);
                btnMinClickImage = ColorHelper.ChangeImageToColorImage(BlackbtnMinClickImage, backColor);
                btnMaxImage = ColorHelper.ChangeImageToColorImage(BlackbtnMaxImage, backColor);
                btnMaxHoverImage = ColorHelper.ChangeImageToColorImage(BlackbtnMaxHoverImage, backColor);
                btnMaxClickImage = ColorHelper.ChangeImageToColorImage(BlackbtnMaxClickImage, backColor);
                btnRestoreImage = ColorHelper.ChangeImageToColorImage(BlackbtnRestoreImage, backColor);
                btnRestoreHoverImage = ColorHelper.ChangeImageToColorImage(BlackbtnRestoreHoverImage, backColor);
                btnRestoreClickImage = ColorHelper.ChangeImageToColorImage(BlackbtnRestoreClickImage, backColor);
                btnCloseImage = ColorHelper.ChangeImageToColorImage(BlackbtnCloseImage, backColor);
                btnCloseHoverImage = ColorHelper.ChangeImageToColorImage(BlackbtnCloseHoverImage, Color.Red);
                btnCloseClickImage = ColorHelper.ChangeImageToColorImage(BlackbtnCloseClickImage, Color.Red);
            }
        }

        private void SetRoundRectWnd()
        {
            Region region = GetRoundRectWnd();
            Point location = frmBase.Location;
            frmBase.Region = region;
            if (frmBase.Location != location)
            {
                frmBase.Location = location;
            }
        }
        internal Region GetRoundRectWnd()
        {
            return GraphicHelper.CreateRoundRectRgn(this.sideWidth, this.formTopOffset, (frmBase.ClientSize.Width + this.sideWidth) + 1, (frmBase.Height - this.sideWidth) + 1, RoundWidth, RoundHeight);
        }

        internal static int topicHeight;
        internal static Bitmap topicImage;
        internal static int sysBtnHeight;
        internal static int sysBtnWidth;
        internal static int btnSpeatorWidth;

        internal static int rightOffset;
        internal static int topOffset;

        internal static Rectangle m_rtIcon;

        internal static Bitmap btnCloseClickImage;
        internal static Bitmap btnCloseHoverImage;
        internal static Bitmap btnCloseImage;
        internal static Bitmap btnMaxClickImage;
        internal static Bitmap btnMaxHoverImage;
        internal static Bitmap btnMaxImage;
        internal static Bitmap btnMinClickImage;
        internal static Bitmap btnMinHoverImage;
        internal static Bitmap btnMinImage;
        internal static Bitmap btnRestoreClickImage;
        internal static Bitmap btnRestoreHoverImage;
        internal static Bitmap btnRestoreImage;

        private static Bitmap BlackbtnMinImage = ResourceHelper.GetBitmapResource("btnMin");
        private static Bitmap BlackbtnMinHoverImage = ResourceHelper.GetBitmapResource("btnMinHover");
        private static Bitmap BlackbtnMinClickImage = ResourceHelper.GetBitmapResource("btnMinClick");
        private static Bitmap BlackbtnMaxImage = ResourceHelper.GetBitmapResource("btnMax");
        private static Bitmap BlackbtnMaxHoverImage = ResourceHelper.GetBitmapResource("btnMaxHover");
        private static Bitmap BlackbtnMaxClickImage = ResourceHelper.GetBitmapResource("btnMaxClick");
        private static Bitmap BlackbtnRestoreImage = ResourceHelper.GetBitmapResource("btnRestore");
        private static Bitmap BlackbtnRestoreHoverImage = ResourceHelper.GetBitmapResource("btnRestoreHover");
        private static Bitmap BlackbtnRestoreClickImage = ResourceHelper.GetBitmapResource("btnRestoreClick");
        private static Bitmap BlackbtnCloseImage = ResourceHelper.GetBitmapResource("btnClose");
        private static Bitmap BlackbtnCloseHoverImage = ResourceHelper.GetBitmapResource("btnCloseHover");
        private static Bitmap BlackbtnCloseClickImage = ResourceHelper.GetBitmapResource("btnCloseClick");

        internal static Color TopicTextColor;
        internal static Font TopicTextFont = new Font("Arial", 9f, FontStyle.Bold);
        internal static StringFormat TopicTextFormat = new StringFormat();

        private int MaxmizeLeftArg = 0;
        private bool IsMaxBefore;
        private Size LastSize;

        //是否按下鼠标
        Boolean IsMouseDown = false;
        //按下时鼠标位置
        Point lastMousePosition;

        private Boolean _IsShowTopic = true;
        public Boolean IsShowTopic
        {
            get
            {
                return _IsShowTopic;
            }
            set
            {
                _IsShowTopic = value;
                frmBase.Invalidate();
            }
        }

        internal int sideHeight
        {
            get
            {
                return this.sideWidth;
            }
        }
        internal int sideWidth
        {
            get
            {
                return ((frmBase.Width - frmBase.ClientSize.Width) / 2);
            }
        }

        internal static int RoundHeight = 2;
        internal static int RoundWidth = 2;

        internal static Color leftSideColor;
        internal static Color rightSideColor;
        internal static float SideBorderTimes;
        internal static float SideLineTimes;

        private Point LastMouesDownPostion;
        private DateTime LastMouseDownTime;

        //是否显示标题栏文字
        private Boolean _IsShowCationText = true;
        public Boolean IsShowCationText
        {
            get
            {
                return _IsShowCationText;
            }
            set
            {
                _IsShowCationText = value;
                frmBase.Invalidate();
            }
        }

        internal int formTopOffset
        {
            get
            {
                return ((frmBase.Height - frmBase.ClientSize.Height) - this.sideHeight);
            }
        }
        internal Color leftSideBorderColor
        {
            get
            {
                return ColorHelper.GetDeeperColor(leftSideColor, SideBorderTimes);
            }
        }

        internal Color leftSideLineColor
        {
            get
            {
                return ColorHelper.GetDeeperColor(leftSideColor, SideLineTimes);
            }
        }

        internal Color rightSideBorderColor
        {
            get
            {
                return ColorHelper.GetDeeperColor(rightSideColor, SideBorderTimes);
            }
        }

        internal Color rightSideLineColor
        {
            get
            {
                return ColorHelper.GetDeeperColor(rightSideColor, SideLineTimes);
            }
        }

        private Rectangle TopicCaptionRectangle
        {
            get
            {
                Rectangle newRect;
                if (frmBase.ShowIcon)
                {
                    newRect = new Rectangle(m_rtIcon.Right + 5, 0, (this.TopicRectangle.Width - ((m_rtIcon.Right + this.sideWidth) + 5)) - (this.TopicRectangle.Right - this.m_rtBtnMin.Left), this.TopicRectangle.Height);
                }
                else
                {
                    newRect = new Rectangle(m_rtIcon.Left + 3, 0, (this.TopicRectangle.Width - ((m_rtIcon.Left + this.sideWidth) + 5)) - (this.TopicRectangle.Right - this.m_rtBtnMin.Left), this.TopicRectangle.Height);
                }
                return newRect;
            }
        }

        internal Rectangle TopicRectangle
        {
            get
            {
                return new Rectangle(1, 0, ((frmBase.Width - (2 * sideWidth)) - 3) + 1, topicHeight);
            }
        }

        internal Rectangle FootRectangle
        {
            get
            {
                return new Rectangle(2, frmBase.ClientSize.Height - 3, (frmBase.ClientSize.Width - 6) + 1, 3);
            }
        }

        private Rectangle m_rtBtnExit
        {
            get
            {
                return new Rectangle((frmBase.ClientSize.Width - ((sysBtnWidth + btnSpeatorWidth) * 1)) - rightOffset, topOffset + 1, sysBtnWidth, sysBtnHeight);
            }
        }

        private Rectangle m_rtBtnMax
        {
            get
            {
                return new Rectangle((frmBase.ClientSize.Width - ((sysBtnWidth + btnSpeatorWidth) * 2)) - rightOffset, topOffset + 1, sysBtnWidth, sysBtnHeight);
            }
        }

        internal Rectangle m_rtBtnMin
        {
            get
            {
                if (!frmBase.MaximizeBox)
                {
                    return this.m_rtBtnMax;
                }
                return new Rectangle((frmBase.ClientSize.Width - ((sysBtnWidth + btnSpeatorWidth) * 3)) - rightOffset, topOffset + 1, sysBtnWidth, sysBtnHeight);
            }
        }

        public void DrawSkin(Graphics fg)
        {
            Rectangle rectangle5;
            Bitmap image = new Bitmap(frmBase.Width, frmBase.Height);
            Graphics graphics = Graphics.FromImage(image);
            Rectangle topicRectangle = this.TopicRectangle;
            if (topicImage == null)
            {
                rectangle5 = new Rectangle(topicRectangle.Left, topicRectangle.Top, topicRectangle.Width, ((topicRectangle.Height * 3) / 8) + 2);
                LinearGradientBrush brush3 = new LinearGradientBrush(rectangle5, Color.White, ColorHelper.GetDeeperColor(skinEng.BackColor, 0.9f), LinearGradientMode.Vertical);
                graphics.FillRectangle(brush3, brush3.Rectangle);
                rectangle5 = new Rectangle(topicRectangle.Left, (topicRectangle.Top + ((topicRectangle.Height * 3) / 8)) - 2, topicRectangle.Width, ((topicRectangle.Height * 5) / 8) + 3);
                brush3 = new LinearGradientBrush(rectangle5, ColorHelper.GetDeeperColor(skinEng.BackColor, 0.9f), ColorHelper.GetDeeperColor(skinEng.BackColor, 1f), LinearGradientMode.Vertical);
                graphics.FillRectangle(brush3, brush3.Rectangle);
            }
            else
            {
                graphics.DrawImage(topicImage, topicRectangle);
            }
            rectangle5 = new Rectangle(0, topicHeight, frmBase.ClientSize.Width / 3, 1);
            LinearGradientBrush brush = new LinearGradientBrush(rectangle5, Color.FromArgb(0, Color.White), Color.FromArgb(100, Color.White), LinearGradientMode.Horizontal);
            graphics.FillRectangle(brush, brush.Rectangle);
            rectangle5 = new Rectangle(frmBase.ClientSize.Width / 3, topicHeight, (frmBase.ClientSize.Width * 2) / 3, 1);
            brush = new LinearGradientBrush(rectangle5, Color.FromArgb(100, Color.White), Color.FromArgb(0, Color.White), LinearGradientMode.Horizontal);
            graphics.FillRectangle(brush, brush.Rectangle);
            if (frmBase.ShowIcon)
            {
                Rectangle rtIcon = m_rtIcon;
                graphics.DrawImage(frmBase.Icon.ToBitmap(), rtIcon);
            }
            if (IsShowCationText)
            {
                SolidBrush brush2 = new SolidBrush(TopicTextColor);
                string displayText = SkinHelpers.GraphicHelper.GetAppropriateString(graphics, frmBase.Text, TopicTextFont, this.TopicCaptionRectangle);
                graphics.DrawString(displayText, TopicTextFont, brush2, this.TopicCaptionRectangle, TopicTextFormat);
            }
            if (topicImage == null)
            {
                Pen pen = new Pen(ColorHelper.GetDeeperColor(skinEng.BackColor, 0.5f));
                graphics.DrawLine(pen, 0, 0, 0, frmBase.ClientSize.Height - 1);
                graphics.DrawLine(pen, frmBase.ClientSize.Width - 1, 0, frmBase.ClientSize.Width - 1, frmBase.ClientSize.Height - 1);
                graphics.DrawLine(pen, 0, 0, frmBase.ClientSize.Width - 1, 0);
                graphics.DrawLine(pen, 1, frmBase.ClientSize.Height - 1, frmBase.ClientSize.Width - 2, frmBase.ClientSize.Height - 1);
                if (frmBase.WindowState == FormWindowState.Normal)
                {
                    graphics.SmoothingMode = SmoothingMode.AntiAlias;
                    pen.Width = 1.6f;
                    rectangle5 = new Rectangle(0, 0, RoundWidth, RoundHeight);
                    graphics.DrawArc(pen, rectangle5, -180f, 90f);
                    rectangle5 = new Rectangle((frmBase.ClientSize.Width - RoundWidth) - 1, 0, RoundWidth, RoundHeight);
                    graphics.DrawArc(pen, rectangle5, -90f, 90f);
                    rectangle5 = new Rectangle((frmBase.ClientSize.Width - RoundWidth) - 1, frmBase.ClientSize.Height - RoundHeight - 1, RoundWidth, RoundHeight);
                    graphics.DrawArc(pen, rectangle5, 0f, 90f);
                    rectangle5 = new Rectangle(0, frmBase.ClientSize.Height - RoundHeight - 1, RoundWidth, RoundHeight);
                    graphics.DrawArc(pen, rectangle5, 90f, 90f);
                }
            }
            else
            {
                Pen pen2 = new Pen(this.leftSideBorderColor);
                pen2.Color = this.leftSideBorderColor;
                graphics.DrawLine(pen2, 0, 0, 0, frmBase.ClientSize.Height - 1);
                pen2.Color = this.leftSideLineColor;
                graphics.DrawLine(pen2, 1, topicHeight, 1, frmBase.ClientSize.Height - 3);
                pen2.Color = leftSideColor;
                int num = 2;
                do
                {
                    graphics.DrawLine(pen2, num, topicHeight, num, frmBase.ClientSize.Height - 2);
                    num++;
                }
                while (num <= 2);
                pen2.Color = this.rightSideBorderColor;
                graphics.DrawLine(pen2, frmBase.ClientSize.Width - 1, 0, frmBase.ClientSize.Width - 1, frmBase.ClientSize.Height);
                pen2.Color = this.rightSideLineColor;
                graphics.DrawLine(pen2, frmBase.ClientSize.Width - 2, topicHeight, frmBase.ClientSize.Width - 2, frmBase.ClientSize.Height - 3);
                pen2.Color = rightSideColor;
                int num2 = 2;
                do
                {
                    graphics.DrawLine(pen2, (frmBase.ClientSize.Width - num2) - 1, topicHeight, (frmBase.ClientSize.Width - num2) - 1, frmBase.ClientSize.Height - 3);
                    num2++;
                }
                while (num2 <= 2);
                LinearGradientBrush brush4 = new LinearGradientBrush(this.FootRectangle, leftSideColor, rightSideColor, LinearGradientMode.Horizontal);
                graphics.FillRectangle(brush4, this.FootRectangle);
                Rectangle rect = new Rectangle(1, frmBase.ClientSize.Height - 2, frmBase.ClientSize.Width - 2, 1);
                brush4 = new LinearGradientBrush(rect, this.leftSideLineColor, this.rightSideLineColor, LinearGradientMode.Horizontal);
                graphics.FillRectangle(brush4, rect);
                rect = new Rectangle(1, frmBase.ClientSize.Height - 1, frmBase.ClientSize.Width - 2, 1);
                brush4 = new LinearGradientBrush(rect, this.leftSideBorderColor, this.rightSideBorderColor, LinearGradientMode.Horizontal);
                graphics.FillRectangle(brush4, rect);
                rectangle5 = new Rectangle(0, 0, frmBase.ClientSize.Width, 1);
                graphics.FillRectangle(brush4, rectangle5);
                if (frmBase.WindowState == FormWindowState.Normal)
                {
                    pen2.Color = this.leftSideBorderColor;
                    pen2.Width = 1.6f;
                    rectangle5 = new Rectangle(0, 0, RoundWidth, RoundHeight);
                    graphics.DrawArc(pen2, rectangle5, -180f, 90f);
                    pen2.Color = this.rightSideBorderColor;
                    pen2.Width = 1.6f;
                    rectangle5 = new Rectangle((frmBase.ClientSize.Width - RoundWidth) - 1, 0, RoundWidth, RoundHeight);
                    graphics.DrawArc(pen2, rectangle5, -90f, 90f);
                }
            }
            if (frmBase.Region != null)
            {
                if (frmBase.MinimizeBox)
                {
                    if (btnMinImage != null)
                    {
                        graphics.DrawImage(btnMinImage, this.m_rtBtnMin);
                    }
                }
                if (frmBase.MaximizeBox)
                {
                    if (frmBase.WindowState == FormWindowState.Normal)
                    {
                        if (btnMaxImage != null)
                        {
                            graphics.DrawImage(btnMaxImage, this.m_rtBtnMax);
                        }
                    }
                    else
                    {
                        if (btnRestoreImage != null)
                        {
                            graphics.DrawImage(btnRestoreImage, this.m_rtBtnMax);
                        }
                    }
                }
                if (btnCloseImage != null)
                {
                    graphics.DrawImage(btnCloseImage, this.m_rtBtnExit);
                }
            }
            if ((frmBase.FormBorderStyle == FormBorderStyle.Sizable) | (frmBase.FormBorderStyle == FormBorderStyle.SizableToolWindow))
            {
                int num3 = 12;
                int num4 = 3;
                Rectangle rectangle4 = new Rectangle((frmBase.ClientSize.Width - 3) - num3, (frmBase.ClientSize.Height - 3) - num3, num3 - num4, num3 - num4);
                Pen pen3 = new Pen(ColorHelper.GetDeeperColor(skinEng.BackColor, 0.5f), 1.5f);
                graphics.DrawLine(pen3, rectangle4.Left, rectangle4.Bottom, rectangle4.Right, rectangle4.Top);
                graphics.DrawLine(pen3, rectangle4.Left + (rectangle4.Width / 3), rectangle4.Bottom, rectangle4.Right, rectangle4.Top + (rectangle4.Height / 3));
                graphics.DrawLine(pen3, rectangle4.Left + ((2 * rectangle4.Width) / 3), rectangle4.Bottom, rectangle4.Right, rectangle4.Top + ((2 * rectangle4.Height) / 3));
            }
            fg.DrawImage(image, 0, 0);
        }
    }
}
